package com.dfbz;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dfbz.entity.User;
import com.dfbz.mapper.UserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

/**
 * @author lscl
 * @version 1.0
 * @intro:
 */
@SpringBootTest(classes = MyBatisPlusApplication.class)
@RunWith(SpringRunner.class)
public class Demo12_ActiveRecord {

    @Autowired
    private UserMapper userMapper;

    /**
     * 新增
     * @throws Exception
     */
    @Test
    public void test1() throws Exception {
        User user = new User(100L,"xiaoming","0",20);
        user.insert();
    }

    /**
     * 删除
     * @throws Exception
     */
    @Test
    public void test2() throws Exception {
        User user = new User();
        user.setId(100L);
        user.deleteById();
    }

    /**
     * 修改
     * @throws Exception
     */
    @Test
    public void test3() throws Exception {
        User user = new User(1L,"xiaohui","1",22);
        user.updateById();
    }

    /**
     * 根据id查询
     * @throws Exception
     */
    @Test
    public void test4() throws Exception {
        User user = new User();
        user.setId(100L);
        User dbUser = user.selectById();
        System.out.println(dbUser);
    }


    /**
     * 查询全部
     * @throws Exception
     */
    @Test
    public void test5() throws Exception {
        User user = new User();
        List<User> userList = user.selectAll();
        for (User dbUser : userList) {
            System.out.println(dbUser);
        }
    }

    /**
     * 根据Wrapper条件查询
     * @throws Exception
     */
    @Test
    public void test6() throws Exception {

        QueryWrapper<User> wrapper = Wrappers.query();
        wrapper.like("name","a");

        User user = new User();

        // 根据条件查询
        List<User> userList = user.selectList(wrapper);
        for (User dbUser : userList) {
            System.out.println(dbUser);
        }
    }

    /**
     * 分页查询
     * @throws Exception
     */
    @Test
    public void test7() throws Exception {

        QueryWrapper<User> wrapper = Wrappers.query();
        wrapper.like("name","a");

        Page<User> page=new Page<>(1,3);
        User user = new User();

        // 分页查询
        user.selectPage(page,wrapper);

        List<User> records = page.getRecords();
        for (User record : records) {
            System.out.println(record);
        }
    }

}
